package com.vichat.common.rudp.thread;

import com.vichat.common.rudp.RudpBlockData;
import com.vichat.common.rudp.RudpDataUtil;
import com.vichat.common.rudp.RudpMonitorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class RudpSendDataManager extends Thread {

    private static transient Logger logger = LoggerFactory.getLogger(RudpSendDataManager.class);

    public RudpSendDataManager() {

    }

    public void run() {
        while (true) {

            ArrayList<String> removeList = new ArrayList<String>();
            int resendNum = 0;
            int allNum = 0;
            int expireNum = 0;
            int successNum = 0;
            try {
                HashMap<String, RudpBlockData> closeData = RudpDataUtil.getResendDate();
                for (Iterator<RudpBlockData> iter = closeData.values().iterator(); iter.hasNext(); ) {
                    long curtime = System.currentTimeMillis();
                    allNum++;
                    try {
                        RudpBlockData bolckData = iter.next();

                        if (bolckData.lifeTime < 0) {
                            successNum++;
                            //成功数据处理
                            removeList.add(bolckData.getBlockKey());
                            if (RudpMonitorUtil.monitorFlag) {
                                RudpMonitorUtil.receiveAck();
                            }
                        } else if (bolckData.firstTime + bolckData.lifeTime * 1000 < curtime) {
                            //过期数据处理
                            expireNum++;
                            removeList.add(bolckData.getBlockKey());
                            if (RudpMonitorUtil.monitorFlag) {
                                RudpMonitorUtil.expireBlock();
                            }
                        } else if (curtime >= bolckData.lastTime + bolckData.interTime) {

                            if (bolckData.resendNums > 6) {
                                expireNum++;
                                removeList.add(bolckData.getBlockKey());
                                if (RudpMonitorUtil.monitorFlag) {
                                    RudpMonitorUtil.expireBlock();
                                }
                                logger.error("重发超过6次,停止重发" + bolckData.getStringTrans() + "-" + bolckData.getBlockKey() + bolckData.desAddress.toString());
                            } else {
                                //重发数据
                                if (bolckData.desAddress != null) {
                                    logger.error("重发数据" + bolckData.getStringTrans() + "-" + bolckData.getBlockKey() + bolckData.desAddress.toString());

                                } else {
                                    logger.error("重发数据" + bolckData.getStringTrans() + "-" + bolckData.getBlockKey());
                                }

                                resendNum++;
                                bolckData.lastTime = curtime;
                                bolckData.resendNums++;
                                RudpDataUtil.sendBlockData(bolckData);
                                if (RudpMonitorUtil.monitorFlag && bolckData.resendNums <= 1) {
                                    RudpMonitorUtil.resendBlock();
                                }
                            }
                        }

                    } catch (Exception e) {
                        logger.error("处理单个数据发送出错", e);
                    }
                }

            } catch (Exception e) {
                logger.error("处理数据发送出错", e);
            }
            try {
                //清除超时数据
                for (Iterator<String> iter = removeList.iterator(); iter.hasNext(); ) {
                    String exiprekey = iter.next();
                    RudpDataUtil.delResendData(exiprekey);
                }
            } catch (Exception e) {
                logger.error("移除发送数据出错", e);
            }
            if (resendNum > 0 || successNum > 0 || expireNum > 0) {
                logger.info("数据包数量,总数" + allNum + ",重发数量" + resendNum + ",成功数量" + successNum + ",过期数量" + expireNum);
            }
            try {
                sleep(300);
            } catch (InterruptedException e) {
                logger.error("处理数据发送出错暂停出错");
            }

        }

    }

}
